<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="uniapp初学习"><meta name="keywords" content="uniapp 小程序"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>uniapp初学习 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#uni-app%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-number">1.</span> <span class="toc-text">uni-app的基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-app%E4%BB%8B%E7%BB%8D-%E5%AE%98%E6%96%B9%E7%BD%91%E9%A1%B5"><span class="toc-number">1.1.</span> <span class="toc-text">uni-app介绍 官方网页</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E9%A1%B9%E7%9B%AE"><span class="toc-number">1.2.</span> <span class="toc-text">运行项目</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D%E9%A1%B9%E7%9B%AE%E7%9B%AE%E5%BD%95%E5%92%8C%E6%96%87%E4%BB%B6%E4%BD%9C%E7%94%A8"><span class="toc-number">1.3.</span> <span class="toc-text">介绍项目目录和文件作用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%9A%E8%BF%87pages%E6%9D%A5%E9%85%8D%E7%BD%AE%E9%A1%B5%E9%9D%A2"><span class="toc-number">1.4.</span> <span class="toc-text">通过pages来配置页面</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEtabbar"><span class="toc-number">1.5.</span> <span class="toc-text">配置tabbar</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#condition%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F%E9%85%8D%E7%BD%AE"><span class="toc-number">1.6.</span> <span class="toc-text">condition启动模式配置</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">组件的基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#text%E6%96%87%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">text文本组件的用法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#001-text-%E7%BB%84%E4%BB%B6%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-number">2.1.1.</span> <span class="toc-text">001 - text 组件的属性</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#002-%E4%BB%A3%E7%A0%81%E6%A1%88%E4%BE%8B"><span class="toc-number">2.1.2.</span> <span class="toc-text">002 - 代码案例</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#view%E8%A7%86%E5%9B%BE%E5%AE%B9%E5%99%A8%E7%BB%84%E4%BB%B6%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-number">2.2.</span> <span class="toc-text">view视图容器组件的用法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#001-%E7%BB%84%E4%BB%B6%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-number">2.2.1.</span> <span class="toc-text">001 - 组件的属性</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#002-%E4%BB%A3%E7%A0%81%E6%A1%88%E4%BE%8B-1"><span class="toc-number">2.2.2.</span> <span class="toc-text">002 - 代码案例</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#button%E6%8C%89%E9%92%AE%E7%BB%84%E4%BB%B6%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-number">2.3.</span> <span class="toc-text">button按钮组件的用法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#001-%E7%BB%84%E4%BB%B6%E7%9A%84%E5%B1%9E%E6%80%A7-1"><span class="toc-number">2.3.1.</span> <span class="toc-text">001 - 组件的属性</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#002-%E6%A1%88%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-number">2.3.2.</span> <span class="toc-text">002 - 案例代码</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#image%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.4.</span> <span class="toc-text">image组件的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#image"><span class="toc-number">2.4.1.</span> <span class="toc-text">image</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni-app%E4%B8%AD%E7%9A%84%E6%A0%B7%E5%BC%8F"><span class="toc-number">3.</span> <span class="toc-text">uni-app中的样式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni-app%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A"><span class="toc-number">4.</span> <span class="toc-text">uni-app中的数据绑定</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8F%92%E5%80%BC%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.1.</span> <span class="toc-text">插值表达式的使用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#v-bind%E5%8A%A8%E6%80%81%E7%BB%91%E5%AE%9A%E5%B1%9E%E6%80%A7"><span class="toc-number">4.2.</span> <span class="toc-text">v-bind动态绑定属性</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#v-for%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.3.</span> <span class="toc-text">v-for的使用</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni%E4%B8%AD%E7%9A%84%E4%BA%8B%E4%BB%B6"><span class="toc-number">5.</span> <span class="toc-text">uni中的事件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-number">5.1.</span> <span class="toc-text">事件绑定</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E4%BC%A0%E5%8F%82"><span class="toc-number">5.2.</span> <span class="toc-text">事件传参</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">6.</span> <span class="toc-text">uni的生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BA%94%E7%94%A8%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">6.1.</span> <span class="toc-text">应用的生命周期</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%A1%B5%E9%9D%A2%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">6.2.</span> <span class="toc-text">页面的生命周期</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0"><span class="toc-number">7.</span> <span class="toc-text">下拉刷新</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BC%80%E5%90%AF%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0"><span class="toc-number">7.1.</span> <span class="toc-text">开启下拉刷新</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%BC%80%E5%90%AF"><span class="toc-number">7.1.1.</span> <span class="toc-text">通过配置文件开启</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%80%9A%E8%BF%87API%E5%BC%80%E5%90%AF"><span class="toc-number">7.1.2.</span> <span class="toc-text">通过API开启</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%9B%91%E5%90%AC%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0"><span class="toc-number">7.2.</span> <span class="toc-text">监听下拉刷新</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%97%AD%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0"><span class="toc-number">7.3.</span> <span class="toc-text">关闭下拉刷新</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8A%E6%8B%89%E5%8A%A0%E8%BD%BD"><span class="toc-number">8.</span> <span class="toc-text">上拉加载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E8%AF%B7%E6%B1%82"><span class="toc-number">9.</span> <span class="toc-text">网络请求</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%BC%93%E5%AD%98"><span class="toc-number">10.</span> <span class="toc-text">数据缓存</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-setStorage"><span class="toc-number">10.1.</span> <span class="toc-text">uni.setStorage</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-setStorageSync"><span class="toc-number">10.2.</span> <span class="toc-text">uni.setStorageSync</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-getStorage"><span class="toc-number">10.3.</span> <span class="toc-text">uni.getStorage</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-getStorageSync"><span class="toc-number">10.4.</span> <span class="toc-text">uni.getStorageSync</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-removeStorage"><span class="toc-number">10.5.</span> <span class="toc-text">uni.removeStorage</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#uni-removeStorageSync"><span class="toc-number">10.6.</span> <span class="toc-text">uni.removeStorageSync</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E3%80%81%E9%A2%84%E8%A7%88%E5%9B%BE%E7%89%87"><span class="toc-number">11.</span> <span class="toc-text">上传图片、预览图片</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87"><span class="toc-number">11.1.</span> <span class="toc-text">上传图片</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%A2%84%E8%A7%88%E5%9B%BE%E7%89%87"><span class="toc-number">11.2.</span> <span class="toc-text">预览图片</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9D%A1%E4%BB%B6%E6%B3%A8%E9%87%8A%E5%AE%9E%E7%8E%B0%E8%B7%A8%E6%AE%B5%E5%85%BC%E5%AE%B9"><span class="toc-number">12.</span> <span class="toc-text">条件注释实现跨段兼容</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%B3%A8%E9%87%8A"><span class="toc-number">12.1.</span> <span class="toc-text">组件的条件注释</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#api%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%B3%A8%E9%87%8A"><span class="toc-number">12.2.</span> <span class="toc-text">api的条件注释</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni%E4%B8%AD%E7%9A%84%E5%AF%BC%E8%88%AA%E8%B7%B3%E8%BD%AC"><span class="toc-number">13.</span> <span class="toc-text">uni中的导航跳转</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%A9%E7%94%A8navigator%E8%BF%9B%E8%A1%8C%E8%B7%B3%E8%BD%AC"><span class="toc-number">13.1.</span> <span class="toc-text">利用navigator进行跳转</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E7%BC%96%E7%A8%8B%E5%BC%8F%E5%AF%BC%E8%88%AA%E8%BF%9B%E8%A1%8C%E8%B7%B3%E8%BD%AC"><span class="toc-number">13.2.</span> <span class="toc-text">利用编程式导航进行跳转</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AF%BC%E8%88%AA%E8%B7%B3%E8%BD%AC%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-number">13.3.</span> <span class="toc-text">导航跳转传递参数</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni-app%E4%B8%AD%E7%BB%84%E4%BB%B6%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-number">14.</span> <span class="toc-text">uni-app中组件的创建</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-number">14.1.</span> <span class="toc-text">组件的生命周期函数</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E9%80%9A%E8%AE%AF"><span class="toc-number">15.</span> <span class="toc-text">组件的通讯</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%88%B6%E7%BB%84%E4%BB%B6%E7%BB%99%E5%AD%90%E7%BB%84%E4%BB%B6%E4%BC%A0%E5%80%BC"><span class="toc-number">15.1.</span> <span class="toc-text">父组件给子组件传值</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AD%90%E7%BB%84%E4%BB%B6%E7%BB%99%E7%88%B6%E7%BB%84%E4%BB%B6%E4%BC%A0%E5%80%BC"><span class="toc-number">15.2.</span> <span class="toc-text">子组件给父组件传值</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%84%E5%BC%9F%E7%BB%84%E4%BB%B6%E9%80%9A%E8%AE%AF"><span class="toc-number">15.3.</span> <span class="toc-text">兄弟组件通讯</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#uni-ui%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">16.</span> <span class="toc-text">uni-ui的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#vuex%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">17.</span> <span class="toc-text">vuex的使用</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">uniapp初学习</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-10-25</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">7.5k</span><span class="post-meta__separator">|</span><span>阅读时长: 31 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>对B站上面的一个uniapp的课程的简单学习记录</p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1y7411p7JC?from=search&seid=841327782786397173">视频地址</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/lookroot/vuestu/tree/master/unibasicdemo">另一个文档地址</a>：介绍的更广泛一些</p>
<p>上面文档的话，其实感觉最重要的还是给我带来了一个关于组件的库，可以更方便的进行使用了</p>
<p><a target="_blank" rel="noopener" href="http://www.uviewui.com/components/intro.html">uview组件库</a>：直接用于uniapp端的组件库，还是比较完善的</p>
<h4 id="uni-app的基本使用"><a href="#uni-app的基本使用" class="headerlink" title="uni-app的基本使用"></a>uni-app的基本使用</h4><p>课程介绍：</p>
<p>基础部分：</p>
<ul>
<li>环境搭建</li>
<li>页面外观配置</li>
<li>数据绑定</li>
<li>uni-app的生命周期</li>
<li>组件的使用</li>
<li>uni-app中样式学习</li>
<li>在uni-app中使用字体图标和开启scss</li>
<li>条件注释跨端兼容</li>
<li>uni中的事件</li>
<li>导航跳转</li>
<li>组件创建和通讯，及组件的生命周期</li>
<li>uni-app中使用uni-ui库</li>
</ul>
<h5 id="uni-app介绍-官方网页"><a href="#uni-app介绍-官方网页" class="headerlink" title="uni-app介绍 官方网页"></a>uni-app介绍 <a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/resource">官方网页</a></h5><h5 id="运行项目"><a href="#运行项目" class="headerlink" title="运行项目"></a>运行项目</h5><p>在菜单栏中点击运行，运行到浏览器，选择浏览器即可运行</p>
<p>在微信开发者工具里运行：进入hello-uniapp项目，点击工具栏的运行 -&gt; 运行到小程序模拟器 -&gt; 微信开发者工具，即可在微信开发者工具里面体验uni-app</p>
<p>在微信开发者工具里运行：进入hello-uniapp项目，点击工具栏的运行 -&gt; 运行到手机或模拟器 -&gt; 选择调式的手机</p>
<p><strong>注意：</strong></p>
<ul>
<li>如果是第一次使用，需要先配置小程序ide的相关路径，才能运行成功</li>
<li>微信开发者工具在设置中安全设置，服务端口开启</li>
</ul>
<h5 id="介绍项目目录和文件作用"><a href="#介绍项目目录和文件作用" class="headerlink" title="介绍项目目录和文件作用"></a>介绍项目目录和文件作用</h5><p><code>pages.json</code> 文件用来对 uni-app 进行<strong>全局配置</strong>，决定页面文件的路径、窗口样式、原生的导航栏、底部的原生tabbar 等，注意，这个文件很重要，含有pages、tabBar、globalStyle等属性</p>
<p><code>manifest.json</code> 文件是<strong>应用的配置文件</strong>，用于指定应用的名称、图标、权限等。</p>
<p><code>App.vue</code>是我们的<strong>根组件</strong>，所有页面都是在<code>App.vue</code>下进行切换的，是页面入口文件，可以调用<strong>应用的</strong>生命周期函数。</p>
<p><code>main.js</code>是我们的<strong>项目入口文件</strong>，主要作用是初始化<code>vue</code>实例并使用需要的插件。</p>
<p><code>uni.scss</code>文件的用途是为了方便整体控制应用的风格。比如按钮颜色、边框风格，<code>uni.scss</code>文件里预置了一批scss变量预置。</p>
<figure class="highlight plaintext"><figcaption><span>就是打包目录，在这里有**各个平台的打包文件**</span></figcaption><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">```pages``` 所有的**页面存放目录**，我们一般在这个目录里面添加页面</span><br><span class="line"></span><br><span class="line">```static``` 静态资源目录，例如图片等</span><br><span class="line"></span><br><span class="line">```components``` **组件存放**目录，所有引入的组件都在这里面</span><br><span class="line"></span><br><span class="line">为了实现多端兼容，综合考虑编译速度、运行性能等因素，`uni-app` 约定了如下开发规范：</span><br><span class="line"></span><br><span class="line">- 页面文件遵循 [Vue 单文件组件 (SFC) 规范](https://vue-loader.vuejs.org/zh/spec.html)</span><br><span class="line">- 组件标签靠近小程序规范，详见[uni-app 组件规范](https://uniapp.dcloud.io/component/README)</span><br><span class="line">- 接口能力（JS API）靠近微信小程序规范，但需将前缀 `wx` 替换为 `uni`，详见[uni-app接口规范](https://uniapp.dcloud.io/api/README)</span><br><span class="line">- 数据绑定及事件处理同 `Vue.js` 规范，同时补充了App及页面的生命周期</span><br><span class="line">- 为兼容多端运行，建议使用flex布局进行开发</span><br><span class="line"></span><br><span class="line">#### 全局配置和页面配置 **在pages.json里面**</span><br><span class="line"></span><br><span class="line">**更具体的配置直接去查官网文档即可**</span><br><span class="line"></span><br><span class="line">##### 通过globalStyle属性进行全局配置</span><br><span class="line"></span><br><span class="line">用于设置应用的状态栏、导航条、标题、窗口背景色等。[详细文档](https://uniapp.dcloud.io/collocation/pages?id=globalstyle)</span><br><span class="line"></span><br><span class="line">| 属性                         | 类型     | 默认值  | 描述                                                         |</span><br><span class="line">| ---------------------------- | -------- | ------- | ------------------------------------------------------------ |</span><br><span class="line">| navigationBarBackgroundColor | HexColor | #F7F7F7 | 导航栏背景颜色（同状态栏背景色）                             |</span><br><span class="line">| navigationBarTextStyle       | String   | white   | 导航栏标题颜色及状态栏前景颜色，仅支持 black/white           |</span><br><span class="line">| navigationBarTitleText       | String   |         | 导航栏标题文字内容                                           |</span><br><span class="line">| backgroundColor              | HexColor | #ffffff | 窗口的背景色                                                 |</span><br><span class="line">| backgroundTextStyle          | String   | dark    | 下拉 loading 的样式，仅支持 dark / light                     |</span><br><span class="line">| enablePullDownRefresh        | Boolean  | false   | 是否开启下拉刷新，详见[页面生命周期](https://uniapp.dcloud.io/use?id=%e9%a1%b5%e9%9d%a2%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f)。 |</span><br><span class="line">| onReachBottomDistance        | Number   | 50      | 页面上拉触底事件触发时距页面底部距离，单位只支持px，详见[页面生命周期](https://uniapp.dcloud.io/use?id=%e9%a1%b5%e9%9d%a2%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f) |</span><br><span class="line"></span><br><span class="line">##### 创建新的message页面</span><br><span class="line"></span><br><span class="line">右键pages目录新建页面，在里面选择基本模板，输入名称，勾选创建同名目录即可自动创建vue文件</span><br><span class="line"></span><br><span class="line">在pages.json文件里面就会**自动将这个页面的路径添加进去**</span><br><span class="line"></span><br><span class="line">```html</span><br><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;view&gt;</span><br><span class="line">		这是信息页面</span><br><span class="line">	&lt;/view&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>

<h5 id="通过pages来配置页面"><a href="#通过pages来配置页面" class="headerlink" title="通过pages来配置页面"></a>通过pages来配置页面</h5><p><strong>相当于vue的路由</strong></p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>默认值</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>path</td>
<td>String</td>
<td></td>
<td>配置页面路径</td>
</tr>
<tr>
<td>style</td>
<td>Object</td>
<td></td>
<td>配置页面窗口表现，配置项参考 <a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/collocation/pages?id=style">pageStyle</a></td>
</tr>
</tbody></table>
<p>pages数组数组中<strong>第一项表示应用启动页</strong></p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">&quot;pages&quot;: [ 、</span><br><span class="line">		&#123;</span><br><span class="line">			&quot;path&quot;:&quot;pages/message/message&quot;</span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">			&quot;path&quot;: &quot;pages/index/index&quot;,</span><br><span class="line">			&quot;style&quot;: &#123;</span><br><span class="line">				&quot;navigationBarTitleText&quot;: &quot;uni-app&quot;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	]</span><br></pre></td></tr></table></figure>

<p>通过style修改页面的标题和导航栏背景色，并且设置h5下拉刷新的特有样式</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;pages&quot;</span>: [ <span class="comment">//pages数组中第一项表示应用启动页，参考：https://uniapp.dcloud.io/collocation/pages</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="string">&quot;path&quot;</span>:<span class="string">&quot;pages/message/message&quot;</span>,</span><br><span class="line">			<span class="string">&quot;style&quot;</span>: &#123;</span><br><span class="line">				<span class="string">&quot;navigationBarBackgroundColor&quot;</span>: <span class="string">&quot;#007AFF&quot;</span>,</span><br><span class="line">				<span class="string">&quot;navigationBarTextStyle&quot;</span>: <span class="string">&quot;white&quot;</span>,</span><br><span class="line">				<span class="string">&quot;enablePullDownRefresh&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">				<span class="string">&quot;disableScroll&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">				<span class="string">&quot;h5&quot;</span>: &#123;</span><br><span class="line">					<span class="string">&quot;pullToRefresh&quot;</span>: &#123;</span><br><span class="line">						<span class="string">&quot;color&quot;</span>: <span class="string">&quot;#007AFF&quot;</span></span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	]</span><br></pre></td></tr></table></figure>

<h5 id="配置tabbar"><a href="#配置tabbar" class="headerlink" title="配置tabbar"></a>配置tabbar</h5><p>如果应用是一个多 tab 应用，可以通过 tabBar 配置项指定 tab 栏的表现，以及 tab 切换时显示的对应页。</p>
<p>类似于vue的公共组件tabbar，但是在uinapp里面没有这个概念，其使用的分页切换的概念</p>
<p><strong>Tips</strong></p>
<ul>
<li>当设置 position 为 top 时，将不会显示 icon</li>
<li>tabBar 中的 list 是一个数组，只能配置最少2个、最多5个 tab，tab 按数组的顺序排序。</li>
</ul>
<p><strong>属性说明：</strong></p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>必填</th>
<th>默认值</th>
<th>描述</th>
<th>平台差异说明</th>
</tr>
</thead>
<tbody><tr>
<td>color</td>
<td>HexColor</td>
<td>是</td>
<td></td>
<td>tab 上的文字默认颜色</td>
<td></td>
</tr>
<tr>
<td>selectedColor</td>
<td>HexColor</td>
<td>是</td>
<td></td>
<td>tab 上的文字选中时的颜色</td>
<td></td>
</tr>
<tr>
<td>backgroundColor</td>
<td>HexColor</td>
<td>是</td>
<td></td>
<td>tab 的背景色</td>
<td></td>
</tr>
<tr>
<td>borderStyle</td>
<td>String</td>
<td>否</td>
<td>black</td>
<td>tabbar 上边框的颜色，仅支持 black/white</td>
<td>App 2.3.4+ 支持其他颜色值</td>
</tr>
<tr>
<td>list</td>
<td>Array</td>
<td>是</td>
<td></td>
<td>tab 的列表，详见 list 属性说明，最少2个、最多5个 tab</td>
<td></td>
</tr>
<tr>
<td>position</td>
<td>String</td>
<td>否</td>
<td>bottom</td>
<td>可选值 bottom、top</td>
<td>top 值仅微信小程序支持</td>
</tr>
</tbody></table>
<p>其中 <strong>list</strong> 接收一个数组，数组中的每个项都是一个对象，其属性值如下：</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>必填</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>pagePath</td>
<td>String</td>
<td>是</td>
<td>页面路径，必须在 pages 中先定义</td>
</tr>
<tr>
<td>text</td>
<td>String</td>
<td>是</td>
<td>tab 上按钮文字，在 5+APP 和 H5 平台为非必填。例如中间可放一个没有文字的+号图标</td>
</tr>
<tr>
<td>iconPath</td>
<td>String</td>
<td>否</td>
<td>图片路径，icon 大小限制为40kb，建议尺寸为 81px * 81px，当 postion 为 top 时，此参数无效，不支持网络图片，不支持字体图标</td>
</tr>
<tr>
<td>selectedIconPath</td>
<td>String</td>
<td>否</td>
<td>选中时的图片路径，icon 大小限制为40kb，建议尺寸为 81px * 81px ，当 postion 为 top 时，此参数无效</td>
</tr>
</tbody></table>
<p>案例代码：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;tabBar&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;list&quot;</span>: [</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="string">&quot;text&quot;</span>: <span class="string">&quot;首页&quot;</span>,</span><br><span class="line">				<span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/index/index&quot;</span>,</span><br><span class="line">				<span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/tabs/home.png&quot;</span>,</span><br><span class="line">				<span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/tabs/home-active.png&quot;</span></span><br><span class="line">			&#125;,</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="string">&quot;text&quot;</span>: <span class="string">&quot;信息&quot;</span>,</span><br><span class="line">				<span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/message/message&quot;</span>,</span><br><span class="line">				<span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/tabs/message.png&quot;</span>,</span><br><span class="line">				<span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/tabs/message-active.png&quot;</span></span><br><span class="line">			&#125;,</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="string">&quot;text&quot;</span>: <span class="string">&quot;我们&quot;</span>,</span><br><span class="line">				<span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/contact/contact&quot;</span>,</span><br><span class="line">				<span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/tabs/contact.png&quot;</span>,</span><br><span class="line">				<span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/tabs/contact-active.png&quot;</span></span><br><span class="line">			&#125;</span><br><span class="line">		]</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h5 id="condition启动模式配置"><a href="#condition启动模式配置" class="headerlink" title="condition启动模式配置"></a>condition启动模式配置</h5><p>启动模式配置，仅开发期间生效，用于模拟直达页面的场景，如：小程序转发后，用户点击所打开的页面。</p>
<p><strong>属性说明：</strong></p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>是否必填</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>current</td>
<td>Number</td>
<td>是</td>
<td>当前激活的模式，list节点的索引值</td>
</tr>
<tr>
<td>list</td>
<td>Array</td>
<td>是</td>
<td>启动模式列表</td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;condition&quot;</span>: &#123; <span class="comment">//模式配置，仅开发期间生效</span></span><br><span class="line">    <span class="string">&quot;current&quot;</span>: <span class="number">0</span>, <span class="comment">//当前激活的模式（list 的索引项）</span></span><br><span class="line">    <span class="string">&quot;list&quot;</span>: [&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: <span class="string">&quot;swiper&quot;</span>, <span class="comment">//模式名称</span></span><br><span class="line">            <span class="string">&quot;path&quot;</span>: <span class="string">&quot;pages/component/swiper/swiper&quot;</span>, <span class="comment">//启动页面，必选</span></span><br><span class="line">            <span class="string">&quot;query&quot;</span>: <span class="string">&quot;interval=4000&amp;autoplay=false&quot;</span> <span class="comment">//启动参数，在页面的onLoad函数里面得到。</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: <span class="string">&quot;test&quot;</span>,</span><br><span class="line">            <span class="string">&quot;path&quot;</span>: <span class="string">&quot;pages/component/switch/switch&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>list说明：</strong></p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>是否必填</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>name</td>
<td>String</td>
<td>是</td>
<td>启动模式名称</td>
</tr>
<tr>
<td>path</td>
<td>String</td>
<td>是</td>
<td>启动页面路径</td>
</tr>
<tr>
<td>query</td>
<td>String</td>
<td>否</td>
<td>启动参数，可在页面的 <a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/use?id=%E9%A1%B5%E9%9D%A2%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F">onLoad</a> 函数里获得</td>
</tr>
</tbody></table>
<h4 id="组件的基本使用"><a href="#组件的基本使用" class="headerlink" title="组件的基本使用"></a>组件的基本使用</h4><p>uni-app提供了丰富的基础组件给开发者，开发者可以像搭积木一样，组合各种组件拼接称自己的应用</p>
<p>uni-app中的组件，就像 <code>HTML</code> 中的 <code>div</code> 、<code>p</code>、<code>span</code> 等标签的作用一样，用于搭建页面的基础结构</p>
<h5 id="text文本组件的用法"><a href="#text文本组件的用法" class="headerlink" title="text文本组件的用法"></a>text文本组件的用法</h5><h6 id="001-text-组件的属性"><a href="#001-text-组件的属性" class="headerlink" title="001 - text 组件的属性"></a>001 - text 组件的属性</h6><table>
<thead>
<tr>
<th align="center">属性</th>
<th align="center">类型</th>
<th align="center">默认值</th>
<th align="center">必填</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">selectable</td>
<td align="center">boolean</td>
<td align="center">false</td>
<td align="center">否</td>
<td align="center">文本是否可选</td>
</tr>
<tr>
<td align="center">space</td>
<td align="center">string</td>
<td align="center">.</td>
<td align="center">否</td>
<td align="center">显示连续空格，可选参数：<code>ensp</code>、<code>emsp</code>、<code>nbsp</code></td>
</tr>
<tr>
<td align="center">decode</td>
<td align="center">boolean</td>
<td align="center">false</td>
<td align="center">否</td>
<td align="center">是否解码</td>
</tr>
</tbody></table>
<ul>
<li><code>text</code> 组件相当于行内标签、在同一行显示</li>
<li>除了文本节点以外的其他节点都无法长按选中</li>
</ul>
<h6 id="002-代码案例"><a href="#002-代码案例" class="headerlink" title="002 - 代码案例"></a>002 - 代码案例</h6><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 长按文本是否可选 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span> <span class="attr">selectable</span>=<span class="string">&#x27;true&#x27;</span>&gt;</span>来了老弟<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 显示连续空格的方式 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">space</span>=<span class="string">&#x27;ensp&#x27;</span>&gt;</span>来了  老弟<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">space</span>=<span class="string">&#x27;emsp&#x27;</span>&gt;</span>来了  老弟<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">space</span>=<span class="string">&#x27;nbsp&#x27;</span>&gt;</span>来了  老弟<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>skyblue<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 是否解码 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span> <span class="attr">decode</span>=<span class="string">&#x27;true&#x27;</span>&gt;</span><span class="symbol">&amp;nbsp;</span> <span class="symbol">&amp;lt;</span> <span class="symbol">&amp;gt;</span> <span class="symbol">&amp;amp;</span> <span class="symbol">&amp;apos;</span> <span class="symbol">&amp;ensp;</span> <span class="symbol">&amp;emsp;</span><span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="view视图容器组件的用法"><a href="#view视图容器组件的用法" class="headerlink" title="view视图容器组件的用法"></a>view视图容器组件的用法</h5><blockquote>
<p>View 视图容器， 类似于 HTML 中的 div</p>
</blockquote>
<h6 id="001-组件的属性"><a href="#001-组件的属性" class="headerlink" title="001 - 组件的属性"></a>001 - 组件的属性</h6><p><img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/2view.png"></p>
<h6 id="002-代码案例-1"><a href="#002-代码案例-1" class="headerlink" title="002 - 代码案例"></a>002 - 代码案例</h6><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">&quot;box2&quot;</span> <span class="attr">hover-class</span>=<span class="string">&quot;box2_active&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">&#x27;box1&#x27;</span> <span class="attr">hover-class</span>=<span class="string">&#x27;active&#x27;</span> <span class="attr">hover-stop-propagation</span> <span class="attr">:hover-start-time</span>=<span class="string">&quot;2000&quot;</span> <span class="attr">:hover-stay-time</span>=<span class="string">&#x27;2000&#x27;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="button按钮组件的用法"><a href="#button按钮组件的用法" class="headerlink" title="button按钮组件的用法"></a>button按钮组件的用法</h5><h6 id="001-组件的属性-1"><a href="#001-组件的属性-1" class="headerlink" title="001 - 组件的属性"></a>001 - 组件的属性</h6><table>
<thead>
<tr>
<th align="center">属性名</th>
<th align="center">类型</th>
<th align="center">默认值</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">size</td>
<td align="center">String</td>
<td align="center">default</td>
<td align="center">按钮的大小</td>
</tr>
<tr>
<td align="center">type</td>
<td align="center">String</td>
<td align="center">default</td>
<td align="center">按钮的样式类型</td>
</tr>
<tr>
<td align="center">plain</td>
<td align="center">Boolean</td>
<td align="center">false</td>
<td align="center">按钮是否镂空，背景色透明</td>
</tr>
<tr>
<td align="center">disabled</td>
<td align="center">Boolean</td>
<td align="center">false</td>
<td align="center">是否按钮</td>
</tr>
<tr>
<td align="center">loading</td>
<td align="center">Boolean</td>
<td align="center">false</td>
<td align="center">名称是否带 loading 图标</td>
</tr>
</tbody></table>
<ul>
<li><code>button</code> 组件默认独占一行，设置 <code>size</code> 为 <code>mini</code> 时可以在一行显示多个</li>
</ul>
<h6 id="002-案例代码"><a href="#002-案例代码" class="headerlink" title="002 - 案例代码"></a>002 - 案例代码</h6><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">size</span>=<span class="string">&#x27;mini&#x27;</span> <span class="attr">type</span>=<span class="string">&#x27;primary&#x27;</span>&gt;</span>前端<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">size</span>=<span class="string">&#x27;mini&#x27;</span> <span class="attr">type</span>=<span class="string">&#x27;default&#x27;</span> <span class="attr">disabled</span>=<span class="string">&#x27;true&#x27;</span>&gt;</span>前端<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">size</span>=<span class="string">&#x27;mini&#x27;</span> <span class="attr">type</span>=<span class="string">&#x27;warn&#x27;</span> <span class="attr">loading</span>=<span class="string">&#x27;true&#x27;</span>&gt;</span>前端<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="image组件的使用"><a href="#image组件的使用" class="headerlink" title="image组件的使用"></a>image组件的使用</h5><h6 id="image"><a href="#image" class="headerlink" title="image"></a><a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/component/image?id=image">image</a></h6><p>图片。</p>
<table>
<thead>
<tr>
<th>属性名</th>
<th>类型</th>
<th>默认值</th>
<th>说明</th>
<th>平台差异说明</th>
</tr>
</thead>
<tbody><tr>
<td>src</td>
<td>String</td>
<td></td>
<td>图片资源地址</td>
<td></td>
</tr>
<tr>
<td>mode</td>
<td>String</td>
<td>‘scaleToFill’</td>
<td>图片裁剪、缩放的模式</td>
<td></td>
</tr>
</tbody></table>
<p><strong>Tips</strong></p>
<ul>
<li><code>&lt;image&gt;</code> 组件默认宽度 300px、高度 225px；</li>
<li><code>src</code> 仅支持相对路径、绝对路径，支持 base64 码；</li>
<li>页面结构复杂，css样式太多的情况，使用 image 可能导致样式生效较慢，出现 “闪一下” 的情况，此时设置 <code>image&#123;will-change: transform&#125;</code> ,可优化此问题。</li>
</ul>
<h4 id="uni-app中的样式"><a href="#uni-app中的样式" class="headerlink" title="uni-app中的样式"></a>uni-app中的样式</h4><ul>
<li><p>rpx 即响应式px，一种根据屏幕宽度自适应的动态单位。以750宽的屏幕为基准，750rpx恰好为屏幕宽度。屏幕变宽，rpx 实际显示效果会等比放大。</p>
</li>
<li><p>使用<code>@import</code>语句可以导入外联样式表，<code>@import</code>后跟需要导入的外联样式表的相对路径，用<code>;</code>表示语句结束</p>
</li>
<li><p>支持基本常用的选择器class、id、element等</p>
</li>
<li><p>在 <code>uni-app</code> 中不能使用 <code>*</code> 选择器。</p>
</li>
<li><p><code>page</code> 相当于 <code>body</code> 节点</p>
</li>
<li><p>定义在 App.vue 中的样式为全局样式，作用于每一个页面。在 pages 目录下 的 vue 文件中定义的样式为局部样式，只作用在对应的页面，并会覆盖 App.vue 中相同的选择器。</p>
</li>
<li><p><code>uni-app</code> 支持使用字体图标，使用方式与普通 <code>web</code> 项目相同，需要注意以下几点：</p>
<ul>
<li><p>字体文件小于 40kb，<code>uni-app</code> 会自动将其转化为 base64 格式；</p>
</li>
<li><p>字体文件大于等于 40kb， 需开发者自己转换，否则使用将不生效；</p>
</li>
<li><p>字体文件的引用路径推荐使用以 ~@ 开头的绝对路径。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">@font-face &#123;</span><br><span class="line">    font-family: test1-icon;</span><br><span class="line">    src: url(&#x27;~@/static/iconfont.ttf&#x27;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>如何使用scss或者less</p>
</li>
</ul>
<h4 id="uni-app中的数据绑定"><a href="#uni-app中的数据绑定" class="headerlink" title="uni-app中的数据绑定"></a>uni-app中的数据绑定</h4><p>在页面中需要定义数据，和我们之前的vue一摸一样，直接在data中定义数据即可</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  data () &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">msg</span>: <span class="string">&#x27;hello-uni&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="插值表达式的使用"><a href="#插值表达式的使用" class="headerlink" title="插值表达式的使用"></a>插值表达式的使用</h5><ul>
<li><p>利用插值表达式渲染基本数据</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>在插值表达式中使用三元运算</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span>&#123;&#123; flag ? &#x27;我是真的&#x27;:&#x27;我是假的&#x27; &#125;&#125;<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>基本运算</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span>&#123;&#123;1+1&#125;&#125;<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="v-bind动态绑定属性"><a href="#v-bind动态绑定属性" class="headerlink" title="v-bind动态绑定属性"></a>v-bind动态绑定属性</h5><p>在data中定义了一张图片，我们希望把这张图片渲染到页面上</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  data () &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">img</span>: <span class="string">&#x27;http://destiny001.gitee.io/image/monkey_02.jpg&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>利用v-bind进行渲染</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">image</span> <span class="attr">v-bind:src</span>=<span class="string">&quot;img&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>还可以缩写成:</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">image</span> <span class="attr">:src</span>=<span class="string">&quot;img&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="v-for的使用"><a href="#v-for的使用" class="headerlink" title="v-for的使用"></a>v-for的使用</h5><p>data中定以一个数组，最终将数组渲染到页面上</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">data () &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">arr</span>: [</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;刘能&#x27;</span>, <span class="attr">age</span>: <span class="number">29</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;赵四&#x27;</span>, <span class="attr">age</span>: <span class="number">39</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;宋小宝&#x27;</span>, <span class="attr">age</span>: <span class="number">49</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;小沈阳&#x27;</span>, <span class="attr">age</span>: <span class="number">59</span> &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>利用v-for进行循环</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;view v-<span class="keyword">for</span>=<span class="string">&quot;(item,i) in arr&quot;</span> :key=<span class="string">&quot;i&quot;</span>&gt;名字：&#123;&#123;item.name&#125;&#125;---年龄：&#123;&#123;item.age&#125;&#125;&lt;/view&gt;</span><br></pre></td></tr></table></figure>

<h4 id="uni中的事件"><a href="#uni中的事件" class="headerlink" title="uni中的事件"></a>uni中的事件</h4><h5 id="事件绑定"><a href="#事件绑定" class="headerlink" title="事件绑定"></a>事件绑定</h5><p>在uni中事件绑定和vue中是一样的，通过v-on进行事件的绑定，也可以简写为@</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;tapHandle&quot;</span>&gt;</span>点我啊<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>事件函数定义在methods中</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">methods: &#123;</span><br><span class="line">  tapHandle () &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;真的点我了&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="事件传参"><a href="#事件传参" class="headerlink" title="事件传参"></a>事件传参</h5><ul>
<li><p>默认如果没有传递参数，事件函数第一个形参为事件对象</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// template</span><br><span class="line">&lt;button @click=&quot;tapHandle&quot;&gt;点我啊&lt;/button&gt;</span><br><span class="line">// script</span><br><span class="line">methods: &#123;</span><br><span class="line">  tapHandle (e) &#123;</span><br><span class="line">    console.log(e)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>如果给事件函数传递参数了，则对应的事件函数形参接收的则是传递过来的数据</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// template</span><br><span class="line">&lt;button @click=&quot;tapHandle(1)&quot;&gt;点我啊&lt;/button&gt;</span><br><span class="line">// script</span><br><span class="line">methods: &#123;</span><br><span class="line">  tapHandle (num) &#123;</span><br><span class="line">    console.log(num)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>如果获取事件对象也想传递参数</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// template</span><br><span class="line">&lt;button @click=&quot;tapHandle(1,$event)&quot;&gt;点我啊&lt;/button&gt;</span><br><span class="line">// script</span><br><span class="line">methods: &#123;</span><br><span class="line">  tapHandle (num,e) &#123;</span><br><span class="line">    console.log(num,e)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="uni的生命周期"><a href="#uni的生命周期" class="headerlink" title="uni的生命周期"></a>uni的生命周期</h4><h5 id="应用的生命周期"><a href="#应用的生命周期" class="headerlink" title="应用的生命周期"></a>应用的生命周期</h5><p>生命周期的概念：一个对象从创建、运行、销毁的整个过程被成为生命周期。</p>
<p>生命周期函数：在生命周期中每个阶段会伴随着每一个函数的触发，这些函数被称为生命周期函数</p>
<p><code>uni-app</code> 支持如下应用生命周期函数：</p>
<p>在这里面可携带一个<strong>option</strong>参数，显示一些场景信息，比如是从什么地方进来的（像扫码进来的，小程序进来的）还有一些杂七杂八的场景，总之，这个option可以获取这个应用到底是在什么场景下面，需要干什么</p>
<table>
<thead>
<tr>
<th>函数名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>onLaunch</td>
<td>当<code>uni-app</code> 初始化完成时触发（全局只触发一次）</td>
</tr>
<tr>
<td>onShow</td>
<td>当 <code>uni-app</code> 启动，或从后台进入前台显示</td>
</tr>
<tr>
<td>onHide</td>
<td>当 <code>uni-app</code> 从前台进入后台</td>
</tr>
<tr>
<td>onError</td>
<td>当 <code>uni-app</code> 报错时触发</td>
</tr>
</tbody></table>
<h5 id="页面的生命周期"><a href="#页面的生命周期" class="headerlink" title="页面的生命周期"></a>页面的生命周期</h5><p><code>uni-app</code> 支持如下页面生命周期函数：</p>
<table>
<thead>
<tr>
<th>函数名</th>
<th>说明</th>
<th>平台差异说明</th>
<th>最低版本</th>
</tr>
</thead>
<tbody><tr>
<td>onLoad</td>
<td>监听页面加载，其参数为上个页面传递的数据，参数类型为Object（用于页面传参），参考<a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/api/router?id=navigateto">示例</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td>onShow</td>
<td>监听页面显示。页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面</td>
<td></td>
<td></td>
</tr>
<tr>
<td>onReady</td>
<td>监听页面初次渲染完成。</td>
<td></td>
<td></td>
</tr>
<tr>
<td>onHide</td>
<td>监听页面隐藏</td>
<td></td>
<td></td>
</tr>
<tr>
<td>onUnload</td>
<td>监听页面卸载</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h4 id="下拉刷新"><a href="#下拉刷新" class="headerlink" title="下拉刷新"></a>下拉刷新</h4><h5 id="开启下拉刷新"><a href="#开启下拉刷新" class="headerlink" title="开启下拉刷新"></a>开启下拉刷新</h5><p>在uni-app中有两种方式开启下拉刷新</p>
<ul>
<li>需要在 <code>pages.json</code> 里，找到的当前页面的pages节点，并在 <code>style</code> 选项中开启 <code>enablePullDownRefresh</code></li>
<li>通过调用uni.startPullDownRefresh方法来开启下拉刷新</li>
</ul>
<h6 id="通过配置文件开启"><a href="#通过配置文件开启" class="headerlink" title="通过配置文件开启"></a>通过配置文件开启</h6><p>创建list页面进行演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		杭州学科</span><br><span class="line">		<span class="tag">&lt;<span class="name">view</span> <span class="attr">v-for</span>=<span class="string">&quot;(item,index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span><br><span class="line">			&#123;&#123;item&#125;&#125;</span><br><span class="line">		<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">arr</span>: [<span class="string">&#x27;前端&#x27;</span>,<span class="string">&#x27;java&#x27;</span>,<span class="string">&#x27;ui&#x27;</span>,<span class="string">&#x27;大数据&#x27;</span>]</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>通过pages.json文件中找到当前页面的pages节点，并在 <code>style</code> 选项中开启 <code>enablePullDownRefresh</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;path&quot;</span>:<span class="string">&quot;pages/list/list&quot;</span>,</span><br><span class="line">    <span class="string">&quot;style&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;enablePullDownRefresh&quot;</span>: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="通过API开启"><a href="#通过API开启" class="headerlink" title="通过API开启"></a>通过API开启</h6><p><a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/api/ui/pulldown">api文档</a></p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">uni.startPullDownRefresh()</span><br></pre></td></tr></table></figure>

<h5 id="监听下拉刷新"><a href="#监听下拉刷新" class="headerlink" title="监听下拉刷新"></a>监听下拉刷新</h5><p>通过onPullDownRefresh可以监听到下拉刷新的动作</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  data () &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">arr</span>: [<span class="string">&#x27;前端&#x27;</span>,<span class="string">&#x27;java&#x27;</span>,<span class="string">&#x27;ui&#x27;</span>,<span class="string">&#x27;大数据&#x27;</span>]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    startPull () &#123;</span><br><span class="line">      uni.startPullDownRefresh()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  onPullDownRefresh () &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;触发下拉刷新了&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="关闭下拉刷新"><a href="#关闭下拉刷新" class="headerlink" title="关闭下拉刷新"></a>关闭下拉刷新</h5><p>uni.stopPullDownRefresh()</p>
<p>停止当前页面下拉刷新。</p>
<p>案例演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;startPull&quot;</span>&gt;</span>开启下拉刷新<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		杭州学科</span><br><span class="line">		<span class="tag">&lt;<span class="name">view</span> <span class="attr">v-for</span>=<span class="string">&quot;(item,index) in arr&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span></span><br><span class="line">			&#123;&#123;item&#125;&#125;</span><br><span class="line">		<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">arr</span>: [<span class="string">&#x27;前端&#x27;</span>,<span class="string">&#x27;java&#x27;</span>,<span class="string">&#x27;ui&#x27;</span>]</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			startPull () &#123;</span></span><br><span class="line"><span class="javascript">				uni.startPullDownRefresh()</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		</span></span><br><span class="line"><span class="javascript">		onPullDownRefresh () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="built_in">this</span>.arr = []</span></span><br><span class="line"><span class="javascript">			<span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="built_in">this</span>.arr = [<span class="string">&#x27;前端&#x27;</span>,<span class="string">&#x27;java&#x27;</span>,<span class="string">&#x27;ui&#x27;</span>,<span class="string">&#x27;大数据&#x27;</span>]</span></span><br><span class="line"><span class="javascript">				uni.stopPullDownRefresh()</span></span><br><span class="line"><span class="javascript">			&#125;, <span class="number">1000</span>);</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="上拉加载"><a href="#上拉加载" class="headerlink" title="上拉加载"></a>上拉加载</h4><p>通过在pages.json文件中找到当前页面的pages节点下style中配置onReachBottomDistance可以设置距离底部开启加载的距离，默认为50px</p>
<p>通过onReachBottom监听到触底的行为</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;view&gt;</span><br><span class="line">		&lt;button type=&quot;primary&quot; @click=&quot;startPull&quot;&gt;开启下拉刷新&lt;/button&gt;</span><br><span class="line">		杭州学科</span><br><span class="line">		&lt;view v-for=&quot;(item,index) in arr&quot; :key=&quot;index&quot;&gt;</span><br><span class="line">			&#123;&#123;item&#125;&#125;</span><br><span class="line">		&lt;/view&gt;</span><br><span class="line">	&lt;/view&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">	export default &#123;</span><br><span class="line">		data () &#123;</span><br><span class="line">			return &#123;</span><br><span class="line">				arr: [&#x27;前端&#x27;,&#x27;java&#x27;,&#x27;ui&#x27;,&#x27;大数据&#x27;,&#x27;前端&#x27;,&#x27;java&#x27;,&#x27;ui&#x27;,&#x27;大数据&#x27;]</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		onReachBottom () &#123;</span><br><span class="line">			console.log(&#x27;触底了&#x27;)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">	view&#123;</span><br><span class="line">		height: 100px;</span><br><span class="line">		line-height: 100px;</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>

<h4 id="网络请求"><a href="#网络请求" class="headerlink" title="网络请求"></a>网络请求</h4><p>在uni中可以调用uni.request方法进行请求网络请求</p>
<p>需要注意的是：在小程序中网络相关的 API 在使用前需要配置域名白名单。</p>
<p><strong>发送get请求</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;sendGet&quot;</span>&gt;</span>发送请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span></span><br><span class="line">&lt;/template&gt;</span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span></span><br><span class="line"><span class="javascript"><span class="xml">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		<span class="attr">methods</span>: &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			sendGet () &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				uni.request(&#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">					<span class="attr">url</span>: <span class="string">&#x27;http://localhost:8082/api/getlunbo&#x27;</span>,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">					<span class="function"><span class="title">success</span>(<span class="params">res</span>)</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">						<span class="built_in">console</span>.log(res)</span></span></span><br><span class="line"><span class="javascript"><span class="xml">					&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				&#125;)</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">	&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<p><strong>发送post请求</strong></p>
<h4 id="数据缓存"><a href="#数据缓存" class="headerlink" title="数据缓存"></a>数据缓存</h4><h5 id="uni-setStorage"><a href="#uni-setStorage" class="headerlink" title="uni.setStorage"></a><strong>uni.setStorage</strong></h5><p><a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/api/storage/storage?id=setstorage">官方文档</a></p>
<p>将数据存储在本地缓存中指定的 key 中，会覆盖掉原来该 key 对应的内容，这是一个异步接口。</p>
<p>代码演示</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;setStor&quot;</span>&gt;</span>存储数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span></span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span></span><br><span class="line"><span class="javascript"><span class="xml">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		<span class="attr">methods</span>: &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			setStor () &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				uni.setStorage(&#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 	<span class="attr">key</span>: <span class="string">&#x27;id&#x27;</span>,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 	<span class="attr">data</span>: <span class="number">100</span>,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 	success () &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 		<span class="built_in">console</span>.log(<span class="string">&#x27;存储成功&#x27;</span>)</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 	&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				 &#125;)</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">	&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h5 id="uni-setStorageSync"><a href="#uni-setStorageSync" class="headerlink" title="uni.setStorageSync"></a>uni.setStorageSync</h5><p>将 data 存储在本地缓存中指定的 key 中，会覆盖掉原来该 key 对应的内容，这是一个同步接口。</p>
<p>代码演示</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	<span class="xml"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;setStor&quot;</span>&gt;</span>存储数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span></span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span></span><br><span class="line"><span class="javascript"><span class="xml">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		<span class="attr">methods</span>: &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			setStor () &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">				uni.setStorageSync(<span class="string">&#x27;id&#x27;</span>,<span class="number">100</span>)</span></span></span><br><span class="line"><span class="javascript"><span class="xml">			&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">		&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">	&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h5 id="uni-getStorage"><a href="#uni-getStorage" class="headerlink" title="uni.getStorage"></a>uni.getStorage</h5><p>从本地缓存中异步获取指定 key 对应的内容。</p>
<p>代码演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;getStorage&quot;</span>&gt;</span>获取数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">id</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			getStorage () &#123;</span></span><br><span class="line"><span class="javascript">				uni.getStorage(&#123;</span></span><br><span class="line"><span class="javascript">					<span class="attr">key</span>: <span class="string">&#x27;id&#x27;</span>,</span></span><br><span class="line"><span class="javascript">					<span class="attr">success</span>:  <span class="function"><span class="params">res</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">						<span class="built_in">this</span>.id = res.data</span></span><br><span class="line"><span class="javascript">					&#125;</span></span><br><span class="line"><span class="javascript">				&#125;)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="uni-getStorageSync"><a href="#uni-getStorageSync" class="headerlink" title="uni.getStorageSync"></a>uni.getStorageSync</h5><p>从本地缓存中同步获取指定 key 对应的内容。</p>
<p>代码演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;getStorage&quot;</span>&gt;</span>获取数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			getStorage () &#123;</span></span><br><span class="line"><span class="javascript">				<span class="keyword">const</span> id = uni.getStorageSync(<span class="string">&#x27;id&#x27;</span>)</span></span><br><span class="line"><span class="javascript">				<span class="built_in">console</span>.log(id)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="uni-removeStorage"><a href="#uni-removeStorage" class="headerlink" title="uni.removeStorage"></a>uni.removeStorage</h5><p>从本地缓存中异步移除指定 key。</p>
<p>代码演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;removeStorage&quot;</span>&gt;</span>删除数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			removeStorage () &#123;</span></span><br><span class="line"><span class="javascript">				uni.removeStorage(&#123;</span></span><br><span class="line"><span class="javascript">					<span class="attr">key</span>: <span class="string">&#x27;id&#x27;</span>,</span></span><br><span class="line"><span class="javascript">					<span class="attr">success</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">						<span class="built_in">console</span>.log(<span class="string">&#x27;删除成功&#x27;</span>)</span></span><br><span class="line"><span class="javascript">					&#125;</span></span><br><span class="line"><span class="javascript">				&#125;)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="uni-removeStorageSync"><a href="#uni-removeStorageSync" class="headerlink" title="uni.removeStorageSync"></a>uni.removeStorageSync</h5><p>从本地缓存中同步移除指定 key。</p>
<p>代码演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;removeStorage&quot;</span>&gt;</span>删除数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			removeStorage () &#123;</span></span><br><span class="line"><span class="javascript">				uni.removeStorageSync(<span class="string">&#x27;id&#x27;</span>)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="上传图片、预览图片"><a href="#上传图片、预览图片" class="headerlink" title="上传图片、预览图片"></a>上传图片、预览图片</h4><h5 id="上传图片"><a href="#上传图片" class="headerlink" title="上传图片"></a>上传图片</h5><p>uni.chooseImage方法从本地相册选择图片或使用相机拍照。</p>
<p>案例代码</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;chooseImg&quot;</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span>&gt;</span>上传图片<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">image</span> <span class="attr">v-for</span>=<span class="string">&quot;item in imgArr&quot;</span> <span class="attr">:src</span>=<span class="string">&quot;item&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;index&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">imgArr</span>: []</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			chooseImg () &#123;</span></span><br><span class="line"><span class="javascript">				uni.chooseImage(&#123;</span></span><br><span class="line"><span class="javascript">					<span class="attr">count</span>: <span class="number">9</span>,</span></span><br><span class="line"><span class="javascript">					<span class="attr">success</span>: <span class="function"><span class="params">res</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">						<span class="built_in">this</span>.imgArr = res.tempFilePaths</span></span><br><span class="line"><span class="javascript">					&#125;</span></span><br><span class="line"><span class="javascript">				&#125;)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="预览图片"><a href="#预览图片" class="headerlink" title="预览图片"></a>预览图片</h5><p>结构</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">image</span> <span class="attr">v-for</span>=<span class="string">&quot;item in imgArr&quot;</span> <span class="attr">:src</span>=<span class="string">&quot;item&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;previewImg(item)&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>预览图片的方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">previewImg (current) &#123;</span><br><span class="line">  uni.previewImage(&#123;</span><br><span class="line">    <span class="attr">urls</span>: <span class="built_in">this</span>.imgArr,</span><br><span class="line">    current</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="条件注释实现跨段兼容"><a href="#条件注释实现跨段兼容" class="headerlink" title="条件注释实现跨段兼容"></a>条件注释实现跨段兼容</h4><p>条件编译是用特殊的注释作为标记，在编译时根据这些特殊的注释，将注释里面的代码编译到不同平台。</p>
<p><strong>写法：</strong>以 #ifdef 加平台标识 开头，以 #endif 结尾。</p>
<p>平台标识</p>
<table>
<thead>
<tr>
<th>值</th>
<th>平台</th>
<th>参考文档</th>
</tr>
</thead>
<tbody><tr>
<td>APP-PLUS</td>
<td>5+App</td>
<td><a target="_blank" rel="noopener" href="http://www.html5plus.org/doc/">HTML5+ 规范</a></td>
</tr>
<tr>
<td>H5</td>
<td>H5</td>
<td></td>
</tr>
<tr>
<td>MP-WEIXIN</td>
<td>微信小程序</td>
<td><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/api/">微信小程序</a></td>
</tr>
<tr>
<td>MP-ALIPAY</td>
<td>支付宝小程序</td>
<td><a target="_blank" rel="noopener" href="https://docs.alipay.com/mini/developer/getting-started">支付宝小程序</a></td>
</tr>
<tr>
<td>MP-BAIDU</td>
<td>百度小程序</td>
<td><a target="_blank" rel="noopener" href="https://smartprogram.baidu.com/docs/develop/tutorial/codedir/">百度小程序</a></td>
</tr>
<tr>
<td>MP-TOUTIAO</td>
<td>头条小程序</td>
<td><a target="_blank" rel="noopener" href="https://developer.toutiao.com/dev/cn/mini-app/develop/framework/basic-reference/introduction">头条小程序</a></td>
</tr>
<tr>
<td>MP-QQ</td>
<td>QQ小程序</td>
<td>（目前仅cli版支持）</td>
</tr>
<tr>
<td>MP</td>
<td>微信小程序/支付宝小程序/百度小程序/头条小程序/QQ小程序</td>
<td></td>
</tr>
</tbody></table>
<h5 id="组件的条件注释"><a href="#组件的条件注释" class="headerlink" title="组件的条件注释"></a>组件的条件注释</h5><p>代码演示</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- #ifdef H5 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  h5页面会显示</span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- #endif --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- #ifdef MP-WEIXIN --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  微信小程序会显示</span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- #endif --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- #ifdef APP-PLUS --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  app会显示</span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- #endif --&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="api的条件注释"><a href="#api的条件注释" class="headerlink" title="api的条件注释"></a>api的条件注释</h5><p>代码演示</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">onLoad () &#123;</span><br><span class="line">  <span class="comment">//#ifdef MP-WEIXIN</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;微信小程序&#x27;</span>)</span><br><span class="line">  <span class="comment">//#endif</span></span><br><span class="line">  <span class="comment">//#ifdef H5</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;h5页面&#x27;</span>)</span><br><span class="line">  <span class="comment">//#endif</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>样式的条件注释</p>
<p>代码演示</p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* #ifdef H5 */</span></span><br><span class="line">view&#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">background</span>: red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* #endif */</span></span><br><span class="line"><span class="comment">/* #ifdef MP-WEIXIN */</span></span><br><span class="line">view&#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">background</span>: green;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* #endif */</span></span><br></pre></td></tr></table></figure>

<h4 id="uni中的导航跳转"><a href="#uni中的导航跳转" class="headerlink" title="uni中的导航跳转"></a>uni中的导航跳转</h4><h5 id="利用navigator进行跳转"><a href="#利用navigator进行跳转" class="headerlink" title="利用navigator进行跳转"></a>利用navigator进行跳转</h5><p>navigator详细文档：<a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/component/navigator">文档地址</a></p>
<p>跳转到普通页面</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">navigator</span> <span class="attr">url</span>=<span class="string">&quot;/pages/about/about&quot;</span> <span class="attr">hover-class</span>=<span class="string">&quot;navigator-hover&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;default&quot;</span>&gt;</span>跳转到关于页面<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">navigator</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>跳转到tabbar页面</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">navigator</span> <span class="attr">url</span>=<span class="string">&quot;/pages/message/message&quot;</span> <span class="attr">open-type</span>=<span class="string">&quot;switchTab&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;default&quot;</span>&gt;</span>跳转到message页面<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">navigator</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="利用编程式导航进行跳转"><a href="#利用编程式导航进行跳转" class="headerlink" title="利用编程式导航进行跳转"></a>利用编程式导航进行跳转</h5><p><a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/api/router?id=navigateto">导航跳转文档</a></p>
<p><strong>利用navigateTo进行导航跳转</strong></p>
<p>保留当前页面，跳转到应用内的某个页面，使用<code>uni.navigateBack</code>可以返回到原页面。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;goAbout&quot;</span>&gt;</span>跳转到关于页面<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>通过navigateTo方法进行跳转到普通页面</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">goAbout () &#123;</span><br><span class="line">  uni.navigateTo(&#123;</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&#x27;/pages/about/about&#x27;</span>,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>通过switchTab跳转到tabbar页面</strong></p>
<p>跳转到tabbar页面</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;goMessage&quot;</span>&gt;</span>跳转到message页面<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>通过switchTab方法进行跳转</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">goMessage () &#123;</span><br><span class="line">  uni.switchTab(&#123;</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&#x27;/pages/message/message&#x27;</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>redirectTo进行跳转</strong> </p>
<p>关闭当前页面，跳转到应用内的某个页面。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- template --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;goMessage&quot;</span>&gt;</span>跳转到message页面<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- js --&gt;</span></span><br><span class="line">goMessage () &#123;</span><br><span class="line">  uni.switchTab(&#123;</span><br><span class="line">    url: &#x27;/pages/message/message&#x27;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过onUnload测试当前组件确实卸载</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">onUnload () &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;组件卸载了&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="导航跳转传递参数"><a href="#导航跳转传递参数" class="headerlink" title="导航跳转传递参数"></a>导航跳转传递参数</h5><p>在导航进行跳转到下一个页面的同时，可以给下一个页面传递相应的参数，接收参数的页面可以通过onLoad生命周期进行接收</p>
<p>传递参数的页面</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">goAbout () &#123;</span><br><span class="line">  uni.navigateTo(&#123;</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&#x27;/pages/about/about?id=80&#x27;</span>,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>接收参数的页面</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">		onLoad (options) &#123;</span><br><span class="line">			<span class="built_in">console</span>.log(options)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>



<h4 id="uni-app中组件的创建"><a href="#uni-app中组件的创建" class="headerlink" title="uni-app中组件的创建"></a>uni-app中组件的创建</h4><p>在uni-app中，可以通过创建一个后缀名为vue的文件，即创建一个组件成功，其他组件可以将该组件通过impot的方式导入，在通过components进行注册即可</p>
<ul>
<li><p>创建test组件，在component中创建test目录，然后新建test.vue文件</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;view&gt;</span><br><span class="line">		这是一个自定义组件</span><br><span class="line">	&lt;/view&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure></li>
<li><p>在其他组件中导入该组件并注册</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">import test from &quot;@/components/test/test.vue&quot;</span><br></pre></td></tr></table></figure></li>
<li><p>注册组件</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">components: &#123;test&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>使用组件</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;test&gt;&lt;/test&gt;</span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="组件的生命周期函数"><a href="#组件的生命周期函数" class="headerlink" title="组件的生命周期函数"></a>组件的生命周期函数</h5><table>
<thead>
<tr>
<th>beforeCreate</th>
<th>在实例初始化之后被调用。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#beforeCreate">详见</a></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>created</td>
<td>在实例创建完成后被立即调用。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#created">详见</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td>beforeMount</td>
<td>在挂载开始之前被调用。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#beforeMount">详见</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td>mounted</td>
<td>挂载到实例上去之后调用。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#mounted">详见</a> 注意：此处并不能确定子组件被全部挂载，如果需要子组件完全挂载之后在执行操作可以使用<code>$nextTick</code><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#Vue-nextTick">Vue官方文档</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td>beforeUpdate</td>
<td>数据更新时调用，发生在虚拟 DOM 打补丁之前。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#beforeUpdate">详见</a></td>
<td>仅H5平台支持</td>
<td></td>
</tr>
<tr>
<td>updated</td>
<td>由于数据更改导致的虚拟 DOM 重新渲染和打补丁，在这之后会调用该钩子。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#updated">详见</a></td>
<td>仅H5平台支持</td>
<td></td>
</tr>
<tr>
<td>beforeDestroy</td>
<td>实例销毁之前调用。在这一步，实例仍然完全可用。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#beforeDestroy">详见</a></td>
<td></td>
<td></td>
</tr>
<tr>
<td>destroyed</td>
<td>Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，所有的事件监听器会被移除，所有的子实例也会被销毁。<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#destroyed">详见</a></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h4 id="组件的通讯"><a href="#组件的通讯" class="headerlink" title="组件的通讯"></a>组件的通讯</h4><h5 id="父组件给子组件传值"><a href="#父组件给子组件传值" class="headerlink" title="父组件给子组件传值"></a>父组件给子组件传值</h5><p>通过props来接受外界传递到组件内部的值</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;view&gt;</span><br><span class="line">		这是一个自定义组件 &#123;&#123;msg&#125;&#125;</span><br><span class="line">	&lt;/view&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">	export default &#123;</span><br><span class="line">		props: [&#x27;msg&#x27;]</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>

<p>其他组件在使用login组件的时候传递值</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;view&gt;</span><br><span class="line">		&lt;test :msg=&quot;msg&quot;&gt;&lt;/test&gt;</span><br><span class="line">	&lt;/view&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">	import test from &quot;@/components/test/test.vue&quot;</span><br><span class="line">	export default &#123;</span><br><span class="line">		data () &#123;</span><br><span class="line">			return &#123;</span><br><span class="line">				msg: &#x27;hello&#x27;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		</span><br><span class="line">		components: &#123;test&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h5 id="子组件给父组件传值"><a href="#子组件给父组件传值" class="headerlink" title="子组件给父组件传值"></a>子组件给父组件传值</h5><p>通过$emit触发事件进行传递参数</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		这是一个自定义组件 &#123;&#123;msg&#125;&#125;</span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;sendMsg&quot;</span>&gt;</span>给父组件传值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">status</span>: <span class="string">&#x27;打篮球&#x27;</span></span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">props</span>: &#123;</span></span><br><span class="line"><span class="javascript">			<span class="attr">msg</span>: &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">type</span>: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">				<span class="attr">value</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">			sendMsg () &#123;</span></span><br><span class="line"><span class="javascript">				<span class="built_in">this</span>.$emit(<span class="string">&#x27;myEvent&#x27;</span>,<span class="built_in">this</span>.status);<span class="comment">//对父组件传入的方法进行调用</span></span></span><br><span class="line"><span class="javascript">                uni.$emit(<span class="string">&#x27;onemit&#x27;</span>,msg)<span class="comment">//对uniapp自带的监听函数的调用</span></span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>父组件定义自定义事件并接收参数</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">test</span> <span class="attr">:msg</span>=<span class="string">&quot;msg&quot;</span> @<span class="attr">myEvent</span>=<span class="string">&quot;getMsg&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">test</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">import</span> test <span class="keyword">from</span> <span class="string">&quot;@/components/test/test.vue&quot;</span></span></span><br><span class="line"><span class="javascript">	<span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">		data () &#123;</span></span><br><span class="line"><span class="javascript">			<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">				<span class="attr">msg</span>: <span class="string">&#x27;hello&#x27;</span></span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="title">onload</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//对子组件所有的触发了onemit函数的监听</span></span></span><br><span class="line"><span class="javascript">          uni.$on(<span class="string">&#x27;onemit&#x27;</span>,<span class="function">(<span class="params">rel</span>)=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">              <span class="built_in">console</span>.log(rel);</span></span><br><span class="line"><span class="javascript">          &#125;),</span></span><br><span class="line"><span class="javascript">          <span class="comment">//还有$once</span></span></span><br><span class="line"><span class="javascript">        &#125;,</span></span><br><span class="line"><span class="javascript">		<span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//传入子组件的方法</span></span></span><br><span class="line"><span class="javascript">			getMsg (res) &#123;</span></span><br><span class="line"><span class="javascript">				<span class="built_in">console</span>.log(res)</span></span><br><span class="line"><span class="javascript">			&#125;</span></span><br><span class="line"><span class="javascript">		&#125;,</span></span><br><span class="line"><span class="javascript">		</span></span><br><span class="line"><span class="javascript">		<span class="attr">components</span>: &#123;test&#125;</span></span><br><span class="line"><span class="javascript">	&#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="兄弟组件通讯"><a href="#兄弟组件通讯" class="headerlink" title="兄弟组件通讯"></a>兄弟组件通讯</h5><h4 id="uni-ui的使用"><a href="#uni-ui的使用" class="headerlink" title="uni-ui的使用"></a>uni-ui的使用</h4><p><a target="_blank" rel="noopener" href="https://uniapp.dcloud.io/component/README?id=uniui">uni-ui文档</a></p>
<p>1、进入Grid宫格组件</p>
<p>2、使用HBuilderX导入该组件</p>
<p>3、导入该组件</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">import uniGrid from &quot;@/components/uni-grid/uni-grid.vue&quot;</span><br><span class="line">import uniGridItem from &quot;@/components/uni-grid-item/uni-grid-item.vue&quot;</span><br></pre></td></tr></table></figure>

<p>4、注册组件</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">components: &#123;uniGrid,uniGridItem&#125;</span><br></pre></td></tr></table></figure>

<p>5、使用组件</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">uni-grid</span> <span class="attr">:column</span>=<span class="string">&quot;3&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>文本<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>文本<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>文本<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">uni-grid-item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">uni-grid</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h4 id="vuex的使用"><a href="#vuex的使用" class="headerlink" title="vuex的使用"></a>vuex的使用</h4><p>1.直接新建store目录，在里面新建index.js文件，在里面引入vuex并进行数据添加</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    <span class="attr">state</span>: &#123;&#125;,</span><br><span class="line">    <span class="attr">mutations</span>: &#123;&#125;,</span><br><span class="line">    <span class="attr">actions</span>: &#123;&#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>

<p>2.直接在main.js里面挂载vuex</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//引入vuex</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./store&#x27;</span></span><br><span class="line"><span class="comment">//把vuex定义成全局组件</span></span><br><span class="line">Vue.prototype.$store = store</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line">App.mpType = <span class="string">&#x27;app&#x27;</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    ...App,</span><br><span class="line">    store<span class="comment">//挂载</span></span><br><span class="line">&#125;)</span><br><span class="line">app.$mount()</span><br></pre></td></tr></table></figure>

<p>3.之后在其他文件里面就可以直接使用了</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">        created () &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="built_in">this</span>.$store)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure></div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/10/25/uniapp初学习/">https://chengsong.info/2020/10/25/uniapp初学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/uniapp-%E5%B0%8F%E7%A8%8B%E5%BA%8F/">uniapp 小程序</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/10/27/vue3%E6%96%B0%E7%89%B9%E6%80%A7/"><i class="fa fa-chevron-left">  </i><span>vue3新特性</span></a></div><div class="next-post pull-right"><a href="/2020/10/18/Git%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/"><span>Git简单使用</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>